package com.example.piece.ui.demo

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.therouter.router.Autowired
import com.therouter.router.Route
import com.dds.gestureunlock.fragment.GestureCreateFragment
import com.dds.gestureunlock.fragment.GestureCreateFragment.GestureCreateListener
import com.dds.gestureunlock.fragment.GestureVerifyFragment
import com.dds.gestureunlock.fragment.GestureVerifyFragment.GestureVerifyListener
import com.dds.gestureunlock.vo.ConfigGestureVO
import com.dds.gestureunlock.vo.ResultVerifyVO
import com.example.base.ext.toast
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.TestActivityGestureUnlockBinding
import com.example.piece.tools.GestureUnlock
import com.example.piece.ui.activity.Layer3Activity

@Route(path = RoutePath.TESTActivity.GESTURE_TEST_ACTIVITY)
class GestureUnlockTestActivity :
    Layer3Activity<TestActivityGestureUnlockBinding, ViewDataBinding>() {
    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = false
    override val leftIcon1Res: Int = R.drawable.ic_back
    override val centerLayoutRes: Int = R.layout.test_activity_gesture_unlock
    override val title: String
        get() = H5_TITLE

    @JvmField
    @Autowired(name = Key.DataKey.H5_TITLE)
    var H5_TITLE: String = ""

    @JvmField
    @Autowired(name = Key.DataKey.GESTURE_TYPE)
    var GESTURE_TYPE: Int = 0 //图形解锁类型  0，设置  1，修改 2， 校验
    override fun onClickLeftIcon1(view: View) {
        onBackPressed()

    }

    private var mGestureCreateFragment: GestureCreateFragment? = null
    private var mGestureVerifyFragment: GestureVerifyFragment? = null

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        when (GESTURE_TYPE) {
            0 -> {
                //初始化手势密码
                showCreateGestureLayout()
            }
            1 -> {
                // 修改手势密码
                showModifyGestureLayout()
            }
            2 -> {
                //手势密码认证
                showVerifyGestureLayout()

            }
        }
    }

    /**
     * 显示初始化手势密码的布局
     */
    private fun showCreateGestureLayout() {
        if (mGestureCreateFragment == null) {
            mGestureCreateFragment = GestureCreateFragment()
            mGestureCreateFragment?.setGestureCreateListener(object : GestureCreateListener {
                override fun onCreateFinished(gestureCode: String) {
                    // 创建手势密码完成
                    GestureUnlock.getInstance()
                        .setGestureCode(this@GestureUnlockTestActivity, gestureCode)
                    this@GestureUnlockTestActivity.finish()
                }

                override fun onCreateFailed(result: ResultVerifyVO) {
                    // 创建手势密码失败
                }

                override fun closeLayout() {
                    // 关闭
                }

                override fun onCancel() {
                    // 取消创建手势密码
                    this@GestureUnlockTestActivity.finish()
                }

                override fun onEventOccur(eventCode: Int) {}
            })
        }
        mGestureCreateFragment?.setData(ConfigGestureVO.defaultConfig())
        addFragment( R.id.fragment_container, mGestureCreateFragment!!)
    }

    /**
     * 显示验证手势密码的布局
     */
    private fun showVerifyGestureLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.BLACK

            //底部导航栏
            //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
        }
        if (mGestureVerifyFragment == null) {
            mGestureVerifyFragment = GestureVerifyFragment()
            mGestureVerifyFragment?.setGestureVerifyListener(object : GestureVerifyListener {
                override fun onVerifyResult(result: ResultVerifyVO) {
                    if (result.isFinished) {
                        //验证成功
                        toast( "onVerifyResult:验证成功")
                    } else {
                        toast( "onVerifyResult:验证失败")
                    }
                }

                override fun closeLayout() {
                    this@GestureUnlockTestActivity.finish()
                }

                override fun onStartCreate() {
                    // 开启了手势密码，但是本地被清空了，需要重置
                }

                override fun onCancel() {
                    // 忘记密码使用其他方式进行
                }

                override fun onEventOccur(eventCode: Int) {
                    toast("onEventOccur:$eventCode")
                }
            })
        }
        mGestureVerifyFragment?.setData(ConfigGestureVO.defaultConfig())

        addFragment( R.id.fragment_container, mGestureVerifyFragment!!)

        mGestureVerifyFragment?.setGestureCodeData(GestureUnlock.getInstance()
            .getGestureCodeSet(this))
    }


    private fun showModifyGestureLayout() {
        if (mGestureVerifyFragment == null) {
            mGestureVerifyFragment = GestureVerifyFragment()
            mGestureVerifyFragment?.setGestureVerifyListener(object : GestureVerifyListener {
                override fun onVerifyResult(result: ResultVerifyVO) {
                    if (result.isFinished) {
                        //验证成功
                        toast( "onVerifyResult:验证成功")
                        showCreateGestureLayout()
                    } else {
                        toast( "onVerifyResult:验证失败")
                    }
                }

                override fun closeLayout() {}
                override fun onStartCreate() {
                    // 开启了手势密码，但是本地被清空了，需要重置
                }

                override fun onCancel() {
                    // 忘记密码使用其他方式进行
                }

                override fun onEventOccur(eventCode: Int) {
                    toast("onEventOccur:$eventCode")
                }
            })
        }
        mGestureVerifyFragment?.setData(ConfigGestureVO.defaultConfig())
        addFragment( R.id.fragment_container, mGestureVerifyFragment!!)

        mGestureVerifyFragment?.setGestureCodeData(GestureUnlock.getInstance()
            .getGestureCodeSet(this))
    }



}
